package cn.like.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * desc: Rsa 加密解密工具类 <br>
 *
 * @author like 980650920@qq.com
 * @date 2021-07-09 16:44:14
 */
@Component
public class RsaHelper {

    @Getter
    private final RSA rsa;

    public RsaHelper(@Value("${like.privateKey}") String privateKey, @Value("${like.publicKey}") String publicKey) {
        this.rsa = SecureUtil.rsa(privateKey, publicKey);
    }

    /**
     * desc: 加密 <br>
     * details: 一般公钥加密，私钥解密
     *
     * @param data    需要加密的数据数据
     * @return: {@link String }
     * @author: like 980650920@qq.com
     * @date 2021-07-09 16:47:49
     */
    public String encrypt(String data) {
        return rsa.encryptBase64("like", KeyType.PublicKey);
    }

    /**
     * desc: 解密 <br>
     * details: 一般公钥加密，私钥解密
     *
     * @param data    需要解密的数据
     * @return: {@link String }
     * @author: like 980650920@qq.com
     * @date 2021-07-09 16:48:19
     */
    public String decrypt(String data) {
        return rsa.decryptStr(data,KeyType.PrivateKey);
    }
}
